package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;

public class QuarantineRequestUpdateThread extends BasicThread {

	public interface QuarantineRequestUpdateThreadCallback {
		public void OnQuarantineRequestUpdateCompleted(boolean isNetworkConntected
				, String result, String message);
		public void OnQuarantineRequestUpdatelError(ErrorItem errorItem);
	}

	private String url;
	private QuarantineRequestUpdateThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/

	/*private JSONObject jObj;
	public QuarantineRequestUpdateThread(Context context
			, QuarantineRequestUpdateThreadCallback callback
			, JSONObject jObj, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.jObj = jObj;
	}*/
	
	//HashMap<String, String> map = new HashMap<String, String>();
	String reltData;
	public QuarantineRequestUpdateThread(Context context
			, QuarantineRequestUpdateThreadCallback callback
			, String reltData, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.reltData = reltData;
	}

	@Override
	public void run() {
		String param = "";
		
		Map<String,String> map = new HashMap<String, String>();
		map.put(Const.RESULTDATA, reltData);
		param = parameterReturn(Const.REQ005, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnQuarantineRequestUpdateCompleted(false, "", "통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnQuarantineRequestUpdatelError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			String data = json.getString(Const.DATA);
			
			Log.i("data", "data->"+data);
			
			if(result == 0){	// result 값이 0일 경우
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				callback.OnQuarantineRequestUpdatelError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				
				callback.OnQuarantineRequestUpdateCompleted(true, data, mResult);
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		/*JSONParser jParser = new JSONParser();

		url = Const.MAIN_URL;
		post =  ParameterUtil.getQuarantineUpdate(jObj.toString());
		JSONObject json = jParser.getJSONFromUrl(url, post);
		
		Log.i("url", url);
		Log.i("post", post.toString());
		Log.i("json", json.toString());*/
		
		/*try {		
			
			
		} catch (JSONException e) {
			
		}*/
	}

}
